﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web.UI.WebControls;


    /// <summary>
    /// 强化数据类型
    /// </summary>
    public class DataTypePlus
    {
        /// <summary>
        /// 获取返回值为Decimal格式
        /// </summary>
        /// <param name="thisStr">返回值</param>
        /// <returns>Decimal格式</returns>
        public static decimal requestDecimal(string thisStr)
        {
            if (thisStr == "")
                return 0;
            else if (PageValidate.IsDecimal(thisStr))
                return Convert.ToDecimal(thisStr);
            else
                return 0;

        }
        /// <summary>
        /// 获取返回值为Decimal格式
        /// </summary>
        /// <param name="thisStr">返回值</param>
        /// <returns>Decimal格式</returns>
        public static decimal requestDecimal(Object thisO)
        {
            if (thisO == null || thisO == System.DBNull.Value)
                return 0;
            else return Convert.ToDecimal( thisO);

        }
        /// <summary>
        /// 获取返回值为Int格式
        /// </summary>
        /// <param name="thisStr">返回值</param>
        /// <returns>Int格式</returns>
        public static int requestInt(string thisStr)
        {
            if (thisStr == "")
                return 0;
            else if (PageValidate.IsInt(thisStr))
                return Convert.ToInt32(thisStr);
            else
                return 0;

        }
        public static int requestInt(Object thisO)
        {
            if (thisO == null)
                return 0;
            else return requestInt(thisO.ToString());

        }

        /// <summary>
        /// 获取返回值为string格式
        /// </summary>
        /// <param name="thisStr">返回值</param>
        /// <returns>string格式</returns>
        public static string requestString(Object thisO)
        {
            if (thisO == null)
                return ("");
            else return thisO.ToString();

        }

        /// <summary>
        /// 获取页面request的值，并赋值给returnO
        /// </summary>
        /// <param name="reqO">页面request的值</param>
        /// <param name="returnO">要返回的变量</param>
        public static void getPageRequest(object reqO, ref int returnO)
        {
            returnO = requestInt(reqO);
        }
        /// <summary>
        /// 获取页面request的值，并赋值给returnO
        /// </summary>
        /// <param name="reqO">页面request的值</param>
        /// <param name="returnO">要返回的变量</param>
        public static void getPageRequest(object reqO, ref string returnO)
        {
            returnO = requestString(reqO);
        }

        public static string ctlYear2Year(string y_)
        {
            if (y_ == "") return "1900";
            else return y_;
        }
        public static string ctlMonthDay2MonthDay(string md_)
        {
            if (md_ == "") return "1";
            else return md_;
        }
        public static string ctlYearMonthDay2HourMinuteSecond(string v_)
        {
            if (v_ == "") return "0";
            else return "1";
        }
        public static DateTime requestDateTime(string y_, string m_, string d_)
        {
            return Convert.ToDateTime(ctlYear2Year(y_) + "-" + ctlMonthDay2MonthDay(m_) + "-" + ctlMonthDay2MonthDay(d_) + " " + ctlYearMonthDay2HourMinuteSecond(y_) + ":" + ctlYearMonthDay2HourMinuteSecond(m_) + ":" + ctlYearMonthDay2HourMinuteSecond(d_));
        }
        /// <summary>
        /// 将数字转换成A-Z和数字，以精简长度,主要适合36以内
        /// </summary>
        /// <param name="v_"></param>
        /// <returns></returns>
        public static string int2string(int v_)
        {
            if (v_ <= 26) return StringPlus.Chr(StringPlus.Asc("A") + v_);
            else return (v_ - 27).ToString();
        }

        public static List<ListItem> requestEnumList(Type enumType, bool allAllOption)
        {
            if (enumType.IsEnum == false)
            {
                return null;
            }
            List<ListItem> list = new List<ListItem>();
            if (allAllOption == true)
            {
                list.Add(new ListItem("", "0"));
            }

            Type typeDescription = typeof(DescriptionAttribute);
            System.Reflection.FieldInfo[] fields = enumType.GetFields();
            string strText = string.Empty;
            string strValue = string.Empty;
            foreach (FieldInfo field in fields)
            {
                if (field.IsSpecialName) continue;
                strValue = field.GetRawConstantValue().ToString();
                object[] arr = field.GetCustomAttributes(typeDescription, true);
                if (arr.Length > 0)
                {
                    strText = (arr[0] as DescriptionAttribute).Description;
                }
                else
                {
                    strText = field.Name;
                }

                list.Add(new ListItem(strText, strValue));
            }

            return list;
        }
       public static int GetRandomSeed()
        {
            byte[] bytes = new byte[4];
            System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
            rng.GetBytes(bytes);
            return BitConverter.ToInt32(bytes, 0);
        }
        /// <summary>
        /// 下个周N的日期
        /// </summary>
        /// <param name="weekDay_">周N（周日：0，周一：1）</param>
        /// <returns></returns>
       public static DateTime WeekDay2Date(int weekDay_)
       {
           DateTime anchorDate = DateTime.Now;
           return anchorDate.AddDays(7).AddDays(-Convert.ToInt32(anchorDate.AddDays(7 - weekDay_).DayOfWeek));
       }
    }

